//
//  WmcWaterLabel.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/12.
//

import UIKit

public extension WmcWeatherLabel {
    enum Format: Int {
        case weather
        case temperature
        case weatherTemperature
    }
}

// MARK: - [WmcWeatherLabel]

open class WmcWeatherLabel: WmcBaseSystemFieldLabel {
    // Open

    /// 显示格式
    open var format: Format = .weatherTemperature {
        didSet { showText() }
    }

    /// 配置
    override open func config() {
        text = "获取天气中..."
        // 获取一次
        updateWeather(WmcWeatherManager.share.weather.value)
        // 开始订阅
        WmcWeatherManager.share.weather.subscribe(onNext: { [weak self] weather in
            guard let weakSelf = self,
                  !weakSelf.styleMode.isStatic else { return }
            weakSelf.updateWeather(weather)
        }).disposed(by: rx.disposeBag)
    }

    // Internal

    /// 当前天气
    var currentWeather: WmcWeatherModel?

    /// 子类获取时间的方法
    func updateWeather(_ weather: WmcWeatherModel?) {
        YPGCD.Main { [weak self] in
            guard let weakSelf = self,
                  let weather = weather else { return }
            weakSelf.currentWeather = weather
            weakSelf.showText()
        }
    }

    /// 显示文字
    func showText() {
        // 直接转format
        guard let currentWeather = currentWeather else { return }
        var str = ""
        switch format {
        case .weather:
            if let weather = currentWeather.lives?.first {
                str = "\(weather.weather ?? "")"
            }
        case .temperature:
            if let weather = currentWeather.lives?.first {
                str = "\(weather.temperature ?? "")℃"
            }
        case .weatherTemperature:
            if let weather = currentWeather.lives?.first {
                str = "\(weather.weather ?? "") \(weather.temperature ?? "")℃"
            }
        }
        text = str
    }
}
